package cn.itcast.tlias.com.tliaswork.controller;


import cn.itcast.tlias.com.tliaswork.pojo.Dept;
import cn.itcast.tlias.com.tliaswork.pojo.Result;
import cn.itcast.tlias.com.tliaswork.service.DeptService;
import cn.itcast.tlias.com.tliaswork.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理的controller
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

//    private static Logger log = LoggerFactory.getLogger(DeptController.class);
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)

    @Autowired
    private DeptService mService;

    /**
     * 查询部门方法
     *
     * @return
     */
    @GetMapping
    public Result list() {
        log.info("查询全部的部门数据");
        List<Dept> deptList = mService.list();
        return Result.success(deptList);
    }

    /**
     * 删除部门
     *
     * @param id
     * @return 拼接的参数需要加注解@PathVariable
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable int id) throws Exception {
        log.info("根据Id删除部门：{}", id);
        mService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门
     * <p>
     * 传递的是一个部门的完整信息
     *
     * @param dept
     * @return 传递的是JSON或者bean，需要用@RequestBody
     */
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("新增部门{}", dept);
        mService.add(dept);
        return Result.success();
    }


    /**
     * 修改部门的信息
     * <p>
     *  获取部门的id
     * @param dept
     * @return 传递的是JSON或者bean，需要用@RequestBody
     */
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门{}", dept);
        mService.update(dept);
        return Result.success();
    }
}
